package cn.com.hhrcw.web;

import cn.com.hhrcw.entity.FileDownload;
import cn.com.hhrcw.mapstruct.FileDownloadConvert;
import cn.com.hhrcw.service.IFileDownloadService;
import cn.com.hhrcw.vo.FileDownloadVO;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lixingxing
 */
@RestController
@Slf4j
@RequestMapping("/admin/export")
@Validated
public class ExportFileController extends JeecgController<FileDownload, IFileDownloadService> {
    @Value(value = "${jeecg.path.upload}")
    private String uploadPath;

    @Autowired
    private IFileDownloadService fileDownloadService;

    @Autowired
    private FileDownloadConvert fileDownloadConvert;

    /**
     * 获取文件导出列表
     *
     * @param from
     * @return
     */
    @GetMapping("/list")
    public Result<List<FileDownloadVO>> list(@NotBlank(message = "未知导出地址") String from) {
        List<FileDownload> list = fileDownloadService.lambdaQuery()
            .eq(FileDownload::getFrom, from)
            .orderByDesc(FileDownload::getCreateTime)
            .last("limit 5")
            .list();
        List<FileDownloadVO> collect = list.stream().map(e -> {
            FileDownloadVO vo = fileDownloadConvert.toVO(e);
            String finished = "FINISHED";
            if (StrUtil.equals(e.getState(), finished)) {
                long between = DateUtil.between(e.getCreateTime(), e.getFinishTime(), DateUnit.SECOND);
                double v = between / 60.0;
                int v1 = (int) (between / 60);
                int v2 = (int) ((v - v1) * 60);
                vo.setDuration(v1 + "分" + v2 + "秒");
            }
            return vo;
        }).collect(Collectors.toList());

        return Result.ok(collect);
    }

    @GetMapping("/delete")
    public Result delete(@NotBlank(message = "id必填") String id) {
        FileDownload download = fileDownloadService.getById(id);
        String path = uploadPath + "/public/";
        if (StrUtil.isNotBlank(download.getUrl())) {
            String[] split = StrUtil.split(download.getUrl(), "public/");
            boolean del = FileUtil.del(path + split[1]);
        }
        String exec = "EXEC";
        if (StrUtil.equals(download.getState(), exec)) {
            return Result.error("任务执行中, 请稍后再试");
        }
        fileDownloadService.removeById(id);
        return Result.ok();
    }

}
